package com.jy.wms.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SetAndModifyModel;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.dao.WmsStockMoveMapper;
import com.jy.wms.pojo.*;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: WmsStockMoveService.java
 * @Description: 库存移动管理
 * @Author Yjs
 * @Date Tue Nov 21 17:13:15 GMT+08:00 2017
 */

@Service
public class WmsStockMoveService extends BaseService<WmsStockMoveMapper, WmsStockMove> {
    protected static Logger log = Logger.getLogger(WmsStockMoveService.class);

    /**
     * 添加库存移动
     *
     * @param entity
     * @throws Exception
     */
    @BaseServiceDescribe(moduleName = "库存移动管理", btnName = "添加库存移动信息")
    @Transactional
    public void baseInsert(WmsStockMove entity) throws Exception {
        //flag 判断是否自动确认移动(回滚专用)
        boolean flag = false;
        //wmsStockList1 原库存(回滚专用)
        List<WmsStock> wmsStockList1 = new ArrayList<WmsStock>();
        //wmsStockList2 新库存(回滚专用)
        List<WmsStock> wmsStockList2 = new ArrayList<WmsStock>();
        //wmsStockMoveDetailedList 库存移动明细(回滚专用)
        List<WmsStockMoveDetailed> wmsStockMoveDetailedList = new ArrayList<WmsStockMoveDetailed>();
        //查询货主信息
        WmsOwner wmsOwner = dao.searchHz(entity.getHzid());
        //库内转移是否自动确认(0不自动 1自动)
        String zyzdqr = wmsOwner.getZyzdqr();
        Integer i = 0;
        //库存状态
        String ydZt = "0";
        //库存移动ID
        String uuId = UUIDUtils.uuid();
        WmsStock wmsStock_s = null;
        if ("0".equals(zyzdqr)) {
            flag = false;
            //库存转移不自动确认
            //库存状态(0：初始 1：确认 2：作废)
            ydZt = "0";
            for (WmsStockMoveDetailed wmsStockMoveDetailed : entity.getList()) {
                wmsStockMoveDetailed.setCkid(entity.getCkid());
                wmsStockMoveDetailed.setHzid(entity.getHzid());
                WmsStock wmsStock = dao.searchKc2(wmsStockMoveDetailed.getHpkcid());
                //库存可移动数量=库存数量-带下架数量-带转移数量-冻结数量-破损数量-补货数量
                Integer kydsl = Integer.valueOf(wmsStock.getSl()) - Integer.valueOf(wmsStock.getDxjsl()) - Integer.valueOf(wmsStock.getDzysl()) - Integer.valueOf(wmsStock.getDjsl()) - Integer.valueOf(wmsStock.getPssl()) - Integer.valueOf(wmsStock.getBhsl());
                //页面传的移动数量
                Integer zysl = wmsStockMoveDetailed.getZysl();
                if (zysl > kydsl) {
                    //rollBack(flag, wmsStockList1, wmsStockList2, wmsStockMoveDetailedList);
                    throw new RuntimeException("移动数量不可大于库存可移动数量");
                } else {
                    //移动后库位ID(目标储位)
                    String ydhkwid = wmsStockMoveDetailed.getYdhkwid();
                    //货品编号
                    String hpbh = wmsStockMoveDetailed.getHpbh();
                    //货品批号(货品批次)
                    String hppc = wmsStockMoveDetailed.getHpph();
                    //查询移动后库位ID(目标储位)数据
                    WmsLibraryPosition wmsLibraryPosition = dao.searchMbKw(ydhkwid);
                    //判断库位货品是否可以混放 0不可 1允许
                    if ("0".equals(wmsLibraryPosition.getHphf())) {
                        //查询库位内的货品(是否有相同货品)
                        List<WmsStock> stockList = dao.searchKcHp(ydhkwid, "", "");
                        if (stockList.size() == 1 && stockList.get(0).getHpbh().equals(hpbh)) {
                            //相同货品批次是否可以混放
                            String pchf = wmsLibraryPosition.getPchf();
                            //同货品批次是否可以混放 0不可 1允许
                            if ("0".equals(pchf)) {
                                List<WmsStock> stockList1 = dao.searchKcHp2(ydhkwid, hpbh, "");
                                if (stockList1.size() == 1 && stockList1.get(0).getHppc().equals(hppc)) {
                                    i = addMx2(wmsStock_s, wmsStockMoveDetailed, uuId, flag, wmsStockList2, wmsStockMoveDetailedList);
                                } else if (stockList1.size() == 0) {
                                    i = addMx2(wmsStock_s, wmsStockMoveDetailed, uuId, flag, wmsStockList2, wmsStockMoveDetailedList);
                                } else {
                                    //rollBack(flag, wmsStockList1, wmsStockList2, wmsStockMoveDetailedList);
                                    throw new RuntimeException(wmsLibraryPosition.getMc() + "库位同货品批次不可以混放");
                                }
                            } else if ("1".equals(pchf)) {
                                //同货品批次是否可以混放 0不可 1允许
                                i = addMx2(wmsStock_s, wmsStockMoveDetailed, uuId, flag, wmsStockList2, wmsStockMoveDetailedList);
                            }
                        } else if (stockList.size() == 0) {
                            i = addMx2(wmsStock_s, wmsStockMoveDetailed, uuId, flag, wmsStockList2, wmsStockMoveDetailedList);
                        } else {
                            //rollBack(flag, wmsStockList1, wmsStockList2, wmsStockMoveDetailedList);
                            throw new RuntimeException(wmsLibraryPosition.getMc() + "库位货品不可以混放");
                        }
                    } else if ("1".equals(wmsLibraryPosition.getHphf())) {
                        //判断库位货品是否可以混放 0不可 1允许
                        i = addMx2(wmsStock_s, wmsStockMoveDetailed, uuId, flag, wmsStockList2, wmsStockMoveDetailedList);
                    }
                }
            }
            if (i > 0) {
                entity.setId(uuId);
                entity.setYdzt(ydZt);
                i = dao.baseInsert(entity);
                if (i > 0) {
                    throw new SucessException("添加库存移动成功");
                } else {
                    //rollBack(flag, wmsStockList1, wmsStockList2, wmsStockMoveDetailedList);
                    throw new RuntimeException("添加库存移动失败");
                }
            }
        } else if ("1".equals(zyzdqr)) {
            flag = true;
            //库内转移自动确认
            //库存状态(0：初始 1：确认 2：作废)
            ydZt = "1";
            for (WmsStockMoveDetailed wmsStockMoveDetailed : entity.getList()) {
                wmsStockMoveDetailed.setCkid(entity.getCkid());
                wmsStockMoveDetailed.setHzid(entity.getHzid());
                //库存可移动数量
                Integer kydsl = wmsStockMoveDetailed.getKydsl();
                //页面传的移动数量
                Integer zysl = wmsStockMoveDetailed.getZysl();
                if (zysl > kydsl) {
                    //rollBack(flag, wmsStockList1, wmsStockList2, wmsStockMoveDetailedList);
                    throw new RuntimeException("移动数量不可大于库存可移动数量");
                } else {
                    //移动后库位ID(目标储位)
                    String ydhkwid = wmsStockMoveDetailed.getYdhkwid();
                    //货品编号
                    String hpbh = wmsStockMoveDetailed.getHpbh();
                    //货品批号(货品批次)
                    String hppc = wmsStockMoveDetailed.getHpph();
                    //查询移动后库位ID(目标储位)数据
                    WmsLibraryPosition wmsLibraryPosition = dao.searchMbKw(ydhkwid);
                    //判断库位货品是否可以混放 0不可 1允许
                    if ("0".equals(wmsLibraryPosition.getHphf())) {
                        //查询库位内的货品(是否有相同货品)
                        List<WmsStock> stockList = dao.searchKcHp(ydhkwid, hpbh, "");
                        if (stockList.size() > 0) {
                            //相同货品批次是否可以混放
                            String pchf = wmsLibraryPosition.getPchf();
                            //同货品批次是否可以混放 0不可 1允许
                            if ("0".equals(pchf)) {
                                List<WmsStock> stockList1 = dao.searchKcHp(ydhkwid, hpbh, hppc);
                                if (stockList1.size() > 0) {
                                    i = addMx(wmsStock_s, wmsStockMoveDetailed, uuId, flag, wmsStockList1, wmsStockList2, wmsStockMoveDetailedList);
                                } else {
                                    //rollBack(flag, wmsStockList1, wmsStockList2, wmsStockMoveDetailedList);
                                    throw new RuntimeException(wmsStockMoveDetailed.getShow_ydhkwid() + "库位同货品批次不可以混放");
                                }
                            } else if ("1".equals(pchf)) {
                                //同货品批次是否可以混放 0不可 1允许
                                i = addMx(wmsStock_s, wmsStockMoveDetailed, uuId, flag, wmsStockList1, wmsStockList2, wmsStockMoveDetailedList);
                            }
                        } else {
                            //rollBack(flag, wmsStockList1, wmsStockList2, wmsStockMoveDetailedList);
                            throw new RuntimeException(wmsStockMoveDetailed.getShow_ydhkwid() + "库位货品不可以混放");
                        }
                    } else if ("1".equals(wmsLibraryPosition.getHphf())) {
                        //判断库位货品是否可以混放 0不可 1允许
                        i = addMx(wmsStock_s, wmsStockMoveDetailed, uuId, flag, wmsStockList1, wmsStockList2, wmsStockMoveDetailedList);
                    }
                }
            }
            if (i > 0) {
                entity.setId(uuId);
                entity.setYdzt(ydZt);
                entity.setQrry(entity.getCjry());
                //确认添加
                i = dao.baseInsert2(entity);
                if (i > 0) {
                    throw new SucessException("添加库存移动成功");
                } else {
                    //rollBack(flag, wmsStockList1, wmsStockList2, wmsStockMoveDetailedList);
                    throw new RuntimeException("添加库存移动失败");
                }
            }
        }
    }

    /**
     * 确认添加明细(辅助)
     *
     * @param wmsStockMoveDetailed
     * @param uuId
     * @param flag
     * @param wmsStockList1            原库存
     * @param wmsStockList2            新库存
     * @param wmsStockMoveDetailedList 库存移动明细
     * @return
     * @throws ServiceException
     */
    @Transactional
    public Integer addMx(WmsStock wmsStock_s, WmsStockMoveDetailed wmsStockMoveDetailed, String uuId, boolean flag, List<WmsStock> wmsStockList1, List<WmsStock> wmsStockList2, List<WmsStockMoveDetailed> wmsStockMoveDetailedList) throws Exception {
        wmsStockMoveDetailed.setId(UUIDUtils.uuid());
        //库存移动ID
        wmsStockMoveDetailed.setKcydid(uuId);
        //添加库存移动明细
        Integer i = dao.baseInsertDetail(wmsStockMoveDetailed);
        if (i > 0) {
            wmsStockMoveDetailedList.add(wmsStockMoveDetailed);
            wmsStockMoveDetailedList.add(wmsStockMoveDetailed);
            //移动前货品库存ID
            String hpkcid = wmsStockMoveDetailed.getHpkcid();
            //查询货品所在库存信息
            WmsStock wmsStock = dao.searchKc2(hpkcid);
            //更改库存总数量
            wmsStock.setSl(String.valueOf(wmsStockMoveDetailed.getZysl()));
            //待下架数量
            wmsStock.setDxjsl("0");
            //待转移数量
            wmsStock.setDzysl("0");
            //冻结数量
            wmsStock.setDjsl("0");
            //破损数量
            wmsStock.setPssl("0");
            //补货数量
            wmsStock.setBhsl("0");
            //库位ID
            wmsStock.setKwid(wmsStockMoveDetailed.getYdhkwid());
            wmsStock.setId(UUIDUtils.uuid());
            //保存转移的库存信息
            i = dao.saveKc(wmsStock);
            if (i > 0) {
                log.info("库存确认转移操作(转移新增库存id:" + wmsStock.getId() + ";转移数量:" + wmsStockMoveDetailed.getZysl() + ")");
                wmsStockList2.add(wmsStock);
                //更改库存数量
                i = dao.updateDzysl2(hpkcid, wmsStockMoveDetailed.getZysl());
                if (i > 0) {
                    log.info("库存确认转移操作(被转移库存id:" + hpkcid + ")");
                    wmsStock_s = new WmsStock();
                    wmsStock_s.setId(hpkcid);
                    wmsStock_s.setDzysl(String.valueOf(wmsStockMoveDetailed.getZysl()));
                    wmsStockList1.add(wmsStock_s);
                } else {
                    // rollBack(flag, wmsStockList1, wmsStockList2, wmsStockMoveDetailedList);
                    throw new RuntimeException("更改库存待转移数量失败");
                }
            } else {
                //rollBack(flag, wmsStockList1, wmsStockList2, wmsStockMoveDetailedList);
                throw new RuntimeException("转移的库存信息保存失败");
            }
        } else {
            //rollBack(flag, wmsStockList1, wmsStockList2, wmsStockMoveDetailedList);
            throw new RuntimeException("添加库存移动明细失败");
        }
        return i;
    }

    /**
     * 不确认添加明细(辅助)
     *
     * @param wmsStockMoveDetailed
     * @param uuId
     * @param flag
     * @param wmsStockList1            原库存
     * @param wmsStockMoveDetailedList 库存移动明细
     * @return
     * @throws ServiceException
     */
    @Transactional
    public Integer addMx2(WmsStock wmsStock_s, WmsStockMoveDetailed wmsStockMoveDetailed, String uuId, boolean flag, List<WmsStock> wmsStockList1, List<WmsStockMoveDetailed> wmsStockMoveDetailedList) throws Exception {
        wmsStockMoveDetailed.setId(UUIDUtils.uuid());
        //库存移动ID
        wmsStockMoveDetailed.setKcydid(uuId);
        //添加库存移动明细
        Integer i = dao.baseInsertDetail(wmsStockMoveDetailed);
        if (i > 0) {
            wmsStockMoveDetailedList.add(wmsStockMoveDetailed);
            //移动前货品库存ID
            String hpkcid = wmsStockMoveDetailed.getHpkcid();
            //更改库存数量
            i = dao.updateDzysl(hpkcid, wmsStockMoveDetailed.getZysl());
            if (i > 0) {
                log.info("库存不确认转移操作(被转移库存id:" + hpkcid + ";待转移数量" + wmsStockMoveDetailed.getZysl() + ")");
                wmsStock_s = new WmsStock();
                wmsStock_s.setId(hpkcid);
                wmsStock_s.setDzysl(String.valueOf(wmsStockMoveDetailed.getZysl()));
                wmsStockList1.add(wmsStock_s);
            } else {
                //rollBack(flag, wmsStockList1, null, wmsStockMoveDetailedList);
                throw new RuntimeException("更改库存待转移数量失败");
            }
        } else {
            //rollBack(flag, wmsStockList1, null, wmsStockMoveDetailedList);
            throw new RuntimeException("添加库存移动明细失败");
        }
        return i;
    }

    /**
     * 作废（根据ID 批量作废）
     *
     * @param idList 主键ID列表
     * @return int
     */
    @Transactional
    @BaseServiceDescribe(moduleName = "库存移动管理", btnName = "作废库存移动信息")
    public void baseDeleteBathPrimaryKeys(List<String> idList, String zfry) throws Exception {
        //移动状态 0：未确认 1：已确认 2：作废
        Integer ydzt = 2;
        Integer pd=-1;//用来判断作废
        Integer pd1=-1;
        //循环库存转移ID
        for (String id : idList) {
            //查询库存移动明细信息
            List<WmsStockMoveDetailed> wmsStockMoveDetailedList = dao.searchYdMx2(id);
            if (wmsStockMoveDetailedList.size() > 0) {
                pd1=1;
                for (WmsStockMoveDetailed wmsStockMoveDetailed : wmsStockMoveDetailedList) {
                    //更改库存待转移数量
                    Integer i = dao.updateKcSl2(String.valueOf(wmsStockMoveDetailed.getZysl()), wmsStockMoveDetailed.getHpkcid());
                    if (i > 0) {
                        log.info("作废库存转移操作(作废转移库存id:" + wmsStockMoveDetailed.getHpkcid() + ";作废待转移数量" + wmsStockMoveDetailed.getZysl() + ")");
                        //更改库存移动状态,记录修改人
                        i = dao.baseDeleteBathPrimaryKeys(id, zfry, ydzt);
                        if (i <= 0) {
                            throw new RuntimeException("作废库存移动失败");
                        }
                    } else {
                        throw new RuntimeException("库存待转移数量更改失败");
                    }
                }
            }else{
                pd=wmsStockMoveDetailedList.size();
            }
        }
        if(pd==0&&pd1==-1){
            throw new ServiceException("请选择未处理的数据进行作废");
        }else{
            throw new SucessException("作废库存移动信息成功");
        }
    }

    /**
     * 分页查询
     *
     * @param entity 实体
     * @return Page<WmsStockMove>
     */
    @BaseServiceDescribe(moduleName = "库存移动管理", btnName = "查询库存移动信息")
    public Page<WmsStockMove> baseSelectPage(WmsStockMove entity) throws ServiceException {
        try {
            PageHelper.startPage(entity.getPage(), entity.getRows());
            return (Page<WmsStockMove>) dao.baseSelectPage(entity);
        } catch (Exception e) {
            throw new ServiceException("查询库存移动信息失败", e);
        }
    }

    /**
     * 查看库存转移信息
     *
     * @param id
     * @param ydzt
     * @return
     */
    @BaseServiceDescribe(moduleName = "库存移动管理", btnName = "编辑查询库存移动信息")
    public WmsStockMove getWmsStockMoveById(String id, Integer ydzt) {
        return dao.getWmsStockMoveById(id, ydzt);
    }

    /**
     * 查看库存转移明细信息
     *
     * @param wmsStockMove
     * @return
     */
    public Page<WmsStockMoveDetailed> getWmsStockMoveDetailById(WmsStockMove wmsStockMove) throws Exception {
        try {
            PageHelper.startPage(wmsStockMove.getPage(), wmsStockMove.getRows());
            return (Page<WmsStockMoveDetailed>) dao.getWmsStockMoveDetailById(wmsStockMove.getId());
        } catch (Exception e) {
            throw new ServiceException("查看库存转移明细信息失败", e);
        }
    }

    /**
     * 修改（根据主键ID修改）
     *
     * @param entity 主键ID列表
     * @return int
     */
    @BaseServiceDescribe(moduleName = "库存移动管理", btnName = "编辑库存移动信息")
    public void baseUpdateByPrimaryKey(WmsStockMove entity) throws Exception {
        Integer i = dao.baseUpdateByPrimaryKey(entity);
        if (i > 0) {
            throw new SucessException("编辑库存移动成功");
        } else {
            throw new ServiceException("编辑库存移动失败");
        }
    }

    /**
     * 查询货品信息
     *
     * @param hpbh
     * @return
     */
    public List<WmsGoods> searchHp(String hpbh) {
        return dao.searchHp(hpbh);
    }

    /**
     * 查询属性为“正常/管控”,并且有效的库位
     *
     * @param stringList
     * @param lx
     * @param sfyx
     * @param hzId
     * @param ckId
     * @return
     */
    public List<WmsLibraryPosition> searchKw(List<String> stringList, String lx, String sfyx, String hzId, String ckId, String mc) {
        return dao.searchKw(stringList, lx, sfyx, hzId, ckId, mc);
    }

    /**
     * 查询属性为“正常/管控”,并且有效的库位
     *
     * @param wmsGoodsList
     * @param stringList
     * @param lx
     * @param sfyx
     * @param hzId
     * @param ckId
     * @return
     */
    public List<WmsLibraryPosition> searchKwMx(List<WmsGoods> wmsGoodsList, List<String> stringList, String lx, String sfyx, String hzId, String ckId, String mc) {
        return dao.searchKwMx(wmsGoodsList, stringList, lx, sfyx, hzId, ckId, mc);
    }

    /**
     * 查询可移动库存
     *
     * @param wmsStockMoveDetailed
     * @return
     * @throws ServiceException
     */
    public Page<WmsStockMoveDetailed> searchKc(WmsStockMoveDetailed wmsStockMoveDetailed) throws ServiceException {
        try {
            PageHelper.startPage(wmsStockMoveDetailed.getPage(), wmsStockMoveDetailed.getRows());
            return (Page<WmsStockMoveDetailed>) dao.searchKc(wmsStockMoveDetailed);
        } catch (Exception e) {
            throw new ServiceException("查询可移动库存失败", e);
        }
    }

    /**
     * 库存移动确认
     *
     * @param qrr
     * @param ids
     */
    @Transactional
    public void saveConfirm(String qrr, List<String> ids) throws Exception {
        //移动状态 0：未确认 1：已确认 2：作废
        Integer ydzt = 0;
        //判断移动状态是否全是未确认的数据
        List<WmsStockMove> wmsStockMoveList = dao.searchConfirm(ydzt, ids);
        if (wmsStockMoveList.size() > 0) {
            throw new RuntimeException("请选择未确认的数据");
        } else {
            //wmsStockList1 原库存(回滚专用)
            List<WmsStock> wmsStockList1 = new ArrayList<WmsStock>();
            //wmsStockList2 新库存(回滚专用)
            List<WmsStock> wmsStockList2 = new ArrayList<WmsStock>();
            //wmsStockMoveDetailedList1 库存移动明细(回滚专用)
            List<WmsStockMoveDetailed> wmsStockMoveDetailedList1 = new ArrayList<WmsStockMoveDetailed>();
            WmsStock wmsStock_s = null;
            //查询库存移动明细(根据库存移动主表ID)
            List<WmsStockMoveDetailed> wmsStockMoveDetailedList = dao.searchYdMx(ids);
            for (WmsStockMoveDetailed wmsStockMoveDetailed : wmsStockMoveDetailedList) {
                //查询货品所在库存信息
                WmsStock wmsStock = dao.searchKc2(wmsStockMoveDetailed.getHpkcid());
                String kcId = wmsStock.getId();
                //更改库存总数量
                wmsStock.setSl(String.valueOf(wmsStockMoveDetailed.getZysl()));
                //待下架数量
                wmsStock.setDxjsl("0");
                //待转移数量
                wmsStock.setDzysl("0");
                //冻结数量
                wmsStock.setDjsl("0");
                //破损数量
                wmsStock.setPssl("0");
                //补货数量
                wmsStock.setBhsl("0");
                //库位ID
                wmsStock.setKwid(wmsStockMoveDetailed.getYdhkwid());
                wmsStock.setId(UUIDUtils.uuid());
                //保存转移的库存信息
                Integer i = dao.saveKc(wmsStock);
                if (i > 0) {
                    log.info("库存转移确认操作(转移新增库存id:" + wmsStock.getId() + ";转移数量:" + wmsStockMoveDetailed.getZysl() + ")");
                    wmsStockList2.add(wmsStock);
                    //更改库存数量以及待转移数量
                    i = dao.updateKcSl(kcId, wmsStockMoveDetailed.getZysl());
                    if (i > 0) {
                        log.info("库存转移确认操作(被转移库存id:" + kcId + ")");
                        wmsStock_s = new WmsStock();
                        wmsStock_s.setId(kcId);
                        wmsStock_s.setDzysl(String.valueOf(wmsStockMoveDetailed.getZysl()));
                        wmsStockList1.add(wmsStock_s);
                        //更改库存转移明细确认后生成的库存ID
                        i = dao.updateKcZyMx(wmsStockMoveDetailed.getId(), wmsStock.getId());
                        if (i > 0) {
                            wmsStockMoveDetailedList1.add(wmsStockMoveDetailed);
                        } else {
                            //rollBack2(wmsStockList1, wmsStockList2, wmsStockMoveDetailedList1);
                            throw new RuntimeException("库存移动明细更改失败");
                        }
                    } else {
                        //rollBack2(wmsStockList1, wmsStockList2, wmsStockMoveDetailedList1);
                        throw new RuntimeException("货品库存数量更改失败");
                    }
                } else {
                    //rollBack2(wmsStockList1, wmsStockList2, wmsStockMoveDetailedList1);
                    throw new RuntimeException("转移的库存信息保存失败");
                }
            }
            //更改库存移动状态
            Integer ydzt1 = 1;
            Integer j = dao.updateYdZt(ids, ydzt1, qrr);
            if (j < ids.size()) {
                //rollBack2(wmsStockList1, wmsStockList2, wmsStockMoveDetailedList1);
                throw new RuntimeException("库存移动信息确认失败");
            } else {
                throw new SucessException("库存移动信息确认成功");
            }
        }
    }

    /**
     * 编辑页面添加按钮(添加明细)
     *
     * @param wmsStockMove
     * @throws Exception
     */
    @Transactional
    public void bjSaveMx(WmsStockMove wmsStockMove) throws Exception {
        //wmsStockList1 原库存(回滚专用)
        List<WmsStock> wmsStockList1 = new ArrayList<WmsStock>();
        //wmsStockList2 新库存(回滚专用)
        List<WmsStock> wmsStockList2 = new ArrayList<WmsStock>();
        //wmsStockMoveList 库存移动主表(回滚专用)
        List<WmsStockMove> wmsStockMoveList = new ArrayList<WmsStockMove>();
        //wmsStockMoveDetailedList 库存移动明细(回滚专用)
        List<WmsStockMoveDetailed> wmsStockMoveDetailedList = new ArrayList<WmsStockMoveDetailed>();
        //库存移动状态为未确认(0)的时候才可以编辑
        Integer i = 0;
        for (WmsStockMoveDetailed wmsStockMoveDetailed : wmsStockMove.getList()) {
            wmsStockMoveDetailed.setHzid(wmsStockMove.getHzid());
            wmsStockMoveDetailed.setCkid(wmsStockMove.getCkid());
            WmsStock wmsStock = dao.searchKc2(wmsStockMoveDetailed.getHpkcid());
            //库存可移动数量=库存数量-带下架数量-带转移数量-冻结数量-破损数量-补货数量
            Integer kydsl = Integer.valueOf(wmsStock.getSl()) - Integer.valueOf(wmsStock.getDxjsl()) - Integer.valueOf(wmsStock.getDzysl()) - Integer.valueOf(wmsStock.getDjsl()) - Integer.valueOf(wmsStock.getPssl()) - Integer.valueOf(wmsStock.getBhsl());
            //库存移动ID
            String uuId = UUIDUtils.uuid();
            //页面传的移动数量
            Integer zysl = wmsStockMoveDetailed.getZysl();
            WmsStock wmsStock_s = null;
            if (zysl > kydsl) {
                throw new RuntimeException("移动数量不可大于库存可移动数量");
            } else {
                //移动后库位ID(目标储位)
                String ydhkwid = wmsStockMoveDetailed.getYdhkwid();
                //货品编号
                String hpbh = wmsStockMoveDetailed.getHpbh();
                //货品批号(货品批次)
                String hppc = wmsStockMoveDetailed.getHpph();
                //查询移动后库位ID(目标储位)数据
                WmsLibraryPosition wmsLibraryPosition = dao.searchMbKw(ydhkwid);
                //判断库位货品是否可以混放 0不可 1允许
                if ("0".equals(wmsLibraryPosition.getHphf())) {
                    //查询库位内的货品(是否有相同货品)
                    List<WmsStock> stockList = dao.searchKcHp(ydhkwid, hpbh, "");
                    if (stockList.size() > 0) {
                        //相同货品批次是否可以混放
                        String pchf = wmsLibraryPosition.getPchf();
                        //同货品批次是否可以混放 0不可 1允许
                        if ("0".equals(pchf)) {
                            List<WmsStock> stockList0 = dao.searchKcHp0(ydhkwid);
                            if (stockList0.size() > 0) {
                                List<WmsStock> stockList1 = dao.searchKcHp(ydhkwid, hpbh, hppc);
                                if (stockList1.size() > 0) {
                                    i = addMx2(wmsStock_s, wmsStockMoveDetailed, wmsStockMoveDetailed.getKcydid(), false, wmsStockList1, wmsStockMoveDetailedList);
                                } else {
                                    throw new RuntimeException(wmsStockMoveDetailed.getShow_ydhkwid() + "库位同货品批次不可以混放");
                                }
                            } else {
                                i = addMx2(wmsStock_s, wmsStockMoveDetailed, wmsStockMoveDetailed.getKcydid(), false, wmsStockList1, wmsStockMoveDetailedList);
                            }
                        } else if ("1".equals(pchf)) {
                            //同货品批次是否可以混放 0不可 1允许
                            i = addMx2(wmsStock_s, wmsStockMoveDetailed, wmsStockMoveDetailed.getKcydid(), false, wmsStockList1, wmsStockMoveDetailedList);
                        }
                    } else {
                        throw new RuntimeException(wmsStockMoveDetailed.getShow_ydhkwid() + "库位货品不可以混放");
                    }
                } else if ("1".equals(wmsLibraryPosition.getHphf())) {
                    //判断库位货品是否可以混放 0不可 1允许
                    i = addMx2(wmsStock_s, wmsStockMoveDetailed, wmsStockMoveDetailed.getKcydid(), false, wmsStockList1, wmsStockMoveDetailedList);
                }
            }
        }
    }

    /**
     * 编辑页面（删除明细）
     *
     * @param setAndModifyModel
     */
    @Transactional
    public void bjDeleteMx(SetAndModifyModel setAndModifyModel) throws Exception {
        if (setAndModifyModel.getIds().size() > 0) {
            WmsStockMove wmsStockMove = dao.getWmsStockMoveById(setAndModifyModel.getId(), 1);
            if (wmsStockMove != null) {
                throw new RuntimeException("库存移动已确认,不可编辑");
            } else {
                List<WmsStockMoveDetailed> wmsStockMoveDetailedList = dao.getWmsStockMoveDetailById(setAndModifyModel.getId());
                if (wmsStockMoveDetailedList.size() > setAndModifyModel.getIds().size()) {
                    //移动状态 0：未确认
                    for (String id : setAndModifyModel.getIds()) {
                        WmsStockMoveDetailed wmsStockMoveDetailed = dao.searchYdMx3(id);
                        //更改库存待转移数量
                        Integer i = dao.updateKcSl2(String.valueOf(wmsStockMoveDetailed.getZysl()), wmsStockMoveDetailed.getHpkcid());
                        if (i > 0) {
                            log.info("库存转移编辑操作(编辑库存id:" + wmsStockMoveDetailed.getHpkcid() + ";待转移数量:" + wmsStockMoveDetailed.getZysl() + ")");
                            //删除明细
                            i = dao.deleteYdMx(wmsStockMoveDetailed.getId());
                            if (i > 0) {
                                /*throw new SucessException("库存移动明细删除成功");*/
                            } else {
                                throw new RuntimeException("库存移动明细删除失败");
                            }
                        } else {
                            throw new RuntimeException("库存移动数量更改失败");
                        }
                    }
                } else {
                    throw new RuntimeException("库存移动明细不可全部删除");
                }
            }
        } else {
            throw new RuntimeException("至少选择一条库存移动明细");
        }
    }

    /**
     *  添加(回滚专用)
     * @param flag 判断是否自动确认移动
     * @param wmsStockList1 原库存
     * @param wmsStockList2 新库存
     * @param wmsStockMoveDetailedList 库存移动明细
     */
    /*public void rollBack(boolean flag, List<WmsStock> wmsStockList1, List<WmsStock> wmsStockList2, List<WmsStockMoveDetailed> wmsStockMoveDetailedList) {
        if (flag) {
            //自动确认移动
            for (WmsStock wmsStock1 : wmsStockList1) {
                dao.updateKcSl3(wmsStock1);
            }
        } else {
            //不自动确认移动
            for (WmsStock wmsStock1 : wmsStockList1) {
                dao.updateKcSl4(wmsStock1);
            }
        }
        //删除新库存
        for (WmsStock wmsStock2 : wmsStockList2) {
            dao.deleteKc(wmsStock2.getId());
        }
        //删除移动明细
        for (WmsStockMoveDetailed wmsStockMoveDetailed : wmsStockMoveDetailedList) {
            dao.deleteYdMx(wmsStockMoveDetailed.getId());
        }
    }*/

    /**
     * 确认(回滚专用)
     *
     * @param wmsStockList1            原库存
     * @param wmsStockList2            新库存
     * @param wmsStockMoveDetailedList 库存移动明细
     */
    /*public void rollBack2(List<WmsStock> wmsStockList1, List<WmsStock> wmsStockList2, List<WmsStockMoveDetailed> wmsStockMoveDetailedList) {
        //原库存更改数据
        for (WmsStock wmsStock1 : wmsStockList1) {
            dao.updateKcSl5(wmsStock1);
        }
        //删除新库存
        for (WmsStock wmsStock2 : wmsStockList2) {
            dao.deleteKc(wmsStock2.getId());
        }
        //删除移动明细
        for (WmsStockMoveDetailed wmsStockMoveDetailed : wmsStockMoveDetailedList) {
            dao.updateKcZyMx(wmsStockMoveDetailed.getId(),"0");
        }
    }*/

    /**
     * 导出库存移动信息
     *
     * @param entity
     * @return
     */
    public List<WmsStockMove> exportWmsStockMove(WmsStockMove entity) {
        return dao.exportWmsStockMove(entity);
    }
}
